import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class fmon {
	public static void main(String args[]) {
		System.out.println("File monitor GUI/Commandline version");
		if(args.length==0){
			System.out.println("Launching default GUI");
			MFrame f = new MFrame();
			f.setVisible(true);
		}
		else{
			System.out.println("Monitoring : " + args[0]);
			Monitor watch = new Monitor(args[0],new console_wrapper(),null);
			watch.start();
		}
	}
}

class console_wrapper extends TextField{
	private static final long serialVersionUID = 1L;
	String prev = "";
	public void setText(String txt){ 
		if(txt.compareTo(prev)!=0)
			System.out.println(txt);
		prev = new String(txt);
	}
}

class MPanel extends Panel implements ActionListener{
	private static final long serialVersionUID = -6824632133536136422L;
	TextField path = new TextField(40);
	TextField status = new TextField(15);
	Button watch = new Button("W");
	Button delete = new Button("X");
	Button pick = new Button("?");
	Monitor mon;
	MPanel(){
		super();
		this.setSize(548,30);
		watch.addActionListener(this);
		delete.addActionListener(this);
		pick.addActionListener(this);
		this.setBackground(Color.LIGHT_GRAY);
		this.add(watch);
		this.add(delete);
		this.add(pick);
		this.add(path);
		this.add(status);
	}
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == pick){
			FileDialog fd = new FileDialog((Frame)this.getParent(),"Pick a file or directory");
			fd.setVisible(true);
			path.setText(fd.getDirectory() + fd.getFile());	
		}
		else if(e.getSource() == watch){
			if(watch.getLabel().compareTo("W")==0){
				watch.setLabel("S");
				mon = new Monitor(path.getText(),status,null);
				mon.start();
			}
			else{
				watch.setLabel("W");
				mon.stop();
			}
		}
		else if(e.getSource() == delete){
			MFrame f = (MFrame)this.getParent();
			f.remove(this);
			f.repaint();
		}

	}
}

class MFrame extends Frame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7939199386158170924L;
	Monitor watch;
	Label delta = new Label(); 
	Button quit  = new Button("Quit");
	Button add  = new Button("Add");
	Panel p = new Panel();
	MFrame() {
		quit.addActionListener(this);
		add.addActionListener(this);
		this.setTitle("File Monitor v1.0");
		this.setLayout(new BorderLayout());
		Panel u = new Panel();
		u.setBackground(Color.BLACK);
		delta.setForeground(Color.YELLOW);
		u.add(quit);
		u.add(add);
		u.add(delta);
		add(u,BorderLayout.NORTH);
		this.setSize(550,90);
	}
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == quit){
			this.dispose();
		}
		else if(e.getSource() == add){
			Dimension dim = this.getSize();
			dim.height += 20;
			this.setSize(dim);
			add(new MPanel(),BorderLayout.SOUTH);
			this.repaint();
		}
		
	}
}

class Monitor extends Thread{
	int sleeptime = 30;
	long prevsize = 0;
	String path;
	TextField tf;
	Label d;
	Monitor(String _path, TextField _tf, Label _d){
		super();
		path = _path;
		tf = _tf;
		d = _d;
		if(!(_tf.getClass()==new console_wrapper().getClass()))
				this.setDaemon(true);
	}
	public void run(){
		File f = new File(path);
		while(true){
			if(f.isFile()){
			//	d.setText("d: " + String.valueOf((f.length() - prevsize)) + "B");
				tf.setText(f.length() / 10E5 + "MB");
			//	prevsize = f.length();
			}
			else{
				long size=0;
				for(File k : f.listFiles())	size += k.length();
				tf.setText(size /10E5 + "MB / " + f.listFiles().length + "Files");
			//	d.setText("d: " + String.valueOf((size - prevsize)) + "B");
			//	prevsize = size;
			}
			try {
				sleep(sleeptime);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

